<?php
class SimpleCache
{
    private $cacheDir = 'cache';
	private $expiryInterval = 2592000; //30*24*60*60;

	public function setCacheDir($val)
    {
        $this->cacheDir = $val;
    }
    
	public function setExpiryInterval($val)
    {  
        $this->expiryInterval = $val;
    }
    
	public function exists($key)
	{
        $filename_cache = $this->cacheDir . '/' . $key . '.cache'; //Cache filename
		$filename_info = $this->cacheDir . '/' . $key . '.info'; //Cache info

		if (file_exists($filename_cache) && file_exists($filename_info))
		{
			$cache_time = file_get_contents ($filename_info) + (int)$this->expiryInterval; //Last update time of the cache file
			$time = time(); //Current Time

			$expiry_time = (int)$time; //Expiry time for the cache

			if ((int)$cache_time >= (int)$expiry_time) //Compare last updated and current time
			{
				return true;
			}
		}

		return false;    
	}

	public function get($key)
	{
        $filename_cache = $this->cacheDir . '/' . $key . '.cache'; //Cache filename
        if ($this->exists($key)) //Compare last updated and current time
        {
            return unserialize(file_get_contents($filename_cache));   //Get contents from file
        }
		return null;    
	}

	public function put($key, $content)
	{
        $time = time(); //Current Time

		if (! file_exists($this->cacheDir))
			mkdir($this->cacheDir);

		$filename_cache = $this->cacheDir . '/' . $key . '.cache'; //Cache filename
		$filename_info = $this->cacheDir . '/' . $key . '.info'; //Cache info
        
        if (!unserialize($content)) {
            $content = $this->get($key);
        }
        
		file_put_contents ($filename_cache ,  $content); // save the content
		file_put_contents ($filename_info , $time); // save the time of last cache update    
	}
}